import { _decorator, Component, Enum, Node } from 'cc';
import { UIContianer } from './UIContianer';
import { UIUtlis } from './UIUtlis';
import { UIMgr } from './UIMgr';
import { UIWatcher } from './UIWatcher';
import { UIAnimationType } from './UIConfig';
import { UIAnimation } from './UIAnimation';
const { ccclass, property } = _decorator;

@ccclass('UIBase')
export class UIBase extends Component {
    //[key: string]: any; 
    private _arrWatcher:UIWatcher[] = [];
    protected uiContianer=new UIContianer();
    protected _isDestory:boolean=false;
    @property({type:Enum(UIAnimationType),displayName:'入场动画',tooltip:'入场退场动画,fade淡入淡出...'})
    animType:UIAnimationType = UIAnimationType.none;
    private _uiAnimation:UIAnimation = null;
    get isDestroy(){
        return this._isDestory;
    }
    set isDestroy(destroy:boolean){
        this._isDestory = destroy;
    }
    init(...rest:any[]){
        UIUtlis.findNode(this.node,this.uiContianer);
        this._uiAnimation = new UIAnimation(this);
        this.oninit.apply(this,arguments);
    }
    oninit(...rest:any[]){

    }
    open(...rest:any[]){
        this._uiAnimation.playAnimtion("open",this.animType,()=>this.onEnter.apply(arguments));
        //this.onEnter.apply(arguments);
        //显示出ui
        if(!this.node.active){
            this.node.active = true;
        }
    }
    close(...rest:any[]){
        this._uiAnimation.playAnimtion("close",this.animType,()=>this.onExit.apply(arguments));
        //this.onExit.apply(this,arguments);
        if(this.isDestroy){
            for(let watcher of this._arrWatcher){
                watcher.removeSelf();
            }
            this.node.destroy();
            return;
        }
        if(this.node.active){
            this.node.active=false;
        }
    }
    onEnter(...rest:any[]){

    }
    onExit(...rest:any[]){

    }
    getNode(nodeName:string){
        return this.uiContianer.getNode(nodeName);
    }
    getComp<T>(nodeName:string,compName:string):T{
        return this.uiContianer.getComp(nodeName,compName) as T;
    }
    addEventL(eventName:string,nodeName:string,cb:Function){
        let node=this.getNode(nodeName);
        if(!node){
            return;
        }
        node.on(eventName,cb);
    }
    addClickEventL(nodeName:string,cb:Function){
        this.addEventL("click",nodeName,cb);
    }
    closeUI(uiName:string,isDestroy:boolean= false,...rest:any[]){
        UIMgr.instance.closeUI.apply(UIMgr.instance,arguments);
    }
    openUI(uiName:string,layerName:string="Normal",callback?:()=>void){
        UIMgr.instance.openUI(uiName,layerName,callback);
    }
    handleMsg(){

    }
    bindCb(data:object,key:string,cb:Function,comp?:Component,target?:any){
        target = target||this;
        let watcher = new UIWatcher(data,key,target,cb,comp);
        //存储起来的目的：为了后续如果ui销毁，要把这个watcher从对应dep中删掉。
        this._arrWatcher.push(watcher);
    }
    bindComp(data:object,key:string,comp:Component){
        this.bindCb(data,key,UIUtlis.refreshComp,comp);
    }
}


